<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Bug 1396798: Do not block toplevel data: navigation to image (except svgs)</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<script class="testbody" type="text/javascript">
SpecialPowers.setBoolPref("security.data_uri.block_toplevel_data_uri_navigations", true);
SimpleTest.registerCleanupFunction(() => {
  SpecialPowers.clearUserPref("security.data_uri.block_toplevel_data_uri_navigations");
});

SimpleTest.waitForExplicitFinish();
SimpleTest.requestFlakyTimeout("have to test that top level data:image loading is blocked/allowed");

function test_toplevel_data_image() {
  const DATA_PNG =
    "";
  let win1 = window.open(DATA_PNG);
  let wrappedWin1 = SpecialPowers.wrap(win1);
  setTimeout(function () {
    let images = wrappedWin1.document.getElementsByTagName('img'); 
    is(images.length, 1, "Loading data:image/png should be allowed");
    is(images[0].src, DATA_PNG, "Sanity: img src matches");
    wrappedWin1.close();
    test_toplevel_data_image_svg();
  }, 1000);
}

function test_toplevel_data_image_svg() {
  const DATA_SVG =
    "";
  let win2 = window.open(DATA_SVG);
  // Unfortunately we can't detect whether the window was closed using some event,
  // hence we are constantly polling till we see that win == null.
  // Test times out on failure.
  var win2Closed = setInterval(function() {
    if (win2 == null || win2.closed) {
      clearInterval(win2Closed);
      ok(true, "Loading data:image/svg+xml should be blocked");
      SimpleTest.finish();
    }
  }, 200);
}
// fire up the tests
test_toplevel_data_image();

</script>
</body>
</html>
